<?php
namespace app\shop\controller\api;

use app\shop\library\ApiController;
class Page extends ApiController{
    public function onShow(){
        $id  = '';
        $name = $this->post('name',$this->check()->required());
        $key = $this->getCacheName('page',$name);
        if (is_int($name)){
            $id = $name;
            $name = '';
        }else{
            if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]{2,50}$/', $name)){
                throw new \Exception('页面标识只能由字母、数字、下划线组成，字母开头,长度在3-50个字符之间');
            }
        }

        $cache = $this->app->cache();
        if ($cache->exists($key)){
            return json_decode($cache->get($key)??'{}',true);
        }else{
            $page = $this->model('Page');
            if ($id){
                $page->where('page_id',$id);
            }
            if ($name){
                $page->where('name',$name);
            }

            $data = $page->getRow();
            if (!$data){
                throw new \Exception('页面不存在');
            }else{
                
                $data['content'] = json_decode($data['content'],true) ;
                $data['style'] = json_decode($data['style'],true);
                
                $cache->set($key,json_encode($data));
                return $data;
            }

        }

    }
}